package com.omesoft.hksnore.util.json;

import android.content.Context;
import android.util.Log;

import com.omesoft.hksnore.util.dao.MendaleSleepAllIfc;
import com.omesoft.hksnore.util.dao.MendaleSleepAllIfcImpl;
import com.omesoft.hksnore.util.dao.MendaleSleepIfc;
import com.omesoft.hksnore.util.dao.MendaleSleepIfcImpl;
import com.omesoft.hksnore.util.data.MySharedPreferencesUtil;
import com.omesoft.hksnore.util.entity.Family;
import com.omesoft.hksnore.util.entity.Medix_Pub_Sync_Sleep;
import com.omesoft.hksnore.util.entity.Medix_Pub_Sync_SleepDetail;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author omesoft_xjm 同步数据
 */
public class ToJsonUtil {

    /**
     * 用户个人信息
     *
     * @param context
     * @param family
     * @return
     */

    public static String TAG = "ToJsonUtil";

    public static String user2String(Context context, Family family) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", family.getName());
        map.put("avatar", family.getAvatar());
        String fileName = family.getAvatar();
        Log.v("user2String", "filename::" + fileName);
        String rename;
        if (fileName != null && !fileName.equals("") && !fileName.equals("null")) {
            String[] str = fileName.substring(7).split("/");
            if (str.length == 1) {
                rename = fileName;
            } else {
                if (str[0].equals("pub.medix.cn"))
                    rename = str[str.length - 1];
                else
                    rename = str[str.length - 2] + ".jpg";
            }
        } else {
            rename = null;
        }
        // map.put("avatar", rename);
        //
        map.put("height", family.getHeight());
        map.put("gender", family.getGender());
        map.put("birthday", family.getBirthday());
        map.put("weight", family.getWeight());
        map.put("waistline", family.getWaistline());
        map.put("hipline", family.getHipline());
        map.put("phone", family.getPhone());
        JSONObject object = new JSONObject(map);
        String infoJson = object.toString();
        Log.v("user2String", "infoJson=" + infoJson);
        return infoJson;
    }

    /**
     * 用户个人信息
     *
     * @param context
     * @param family
     * @return
     */
    public static String user2String2(Context context, Family family) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", family.getName());
        map.put("avatar", family.getAvatar());

        //
        map.put("height", family.getHeight());
        map.put("gender", family.getGender());
        map.put("birthday", family.getBirthday());
        map.put("weight", family.getWeight());
        map.put("waistline", family.getWaistline());
        map.put("hipline", family.getHipline());
        map.put("phone", family.getPhone());
        JSONObject object = new JSONObject(map);
        String infoJson = object.toString();
        Log.v("user2String", "infoJson=" + infoJson);
        return infoJson;
    }

    /**
     * 提交睡眠Medix_Pub_Sync_Sleep，Medix_Pub_Sync_SleepDetail两张表的数据<br>
     * 现在新增，提交混音的数据
     *
     * @param context
     * @return
     */
    public static String allSleepInfo2String(Context context) {
        MendaleSleepAllIfc ifc = new MendaleSleepAllIfcImpl(context);
        MendaleSleepIfc mifc = new MendaleSleepIfcImpl(context);


        List<Medix_Pub_Sync_Sleep> Sleeps;
        List<Medix_Pub_Sync_SleepDetail> SleepDetails;
        try {
            Sleeps = ifc.findSleepByMemberId(MySharedPreferencesUtil.getMemberId(context),
                    MySharedPreferencesUtil.getLocalTime(context));
            SleepDetails = mifc.findAllSleepDetaiByMemberId(MySharedPreferencesUtil.getMemberId(context),
                    MySharedPreferencesUtil.getLocalTime(context));

        } catch (Exception e) {
            e.printStackTrace();
            Sleeps = new ArrayList<Medix_Pub_Sync_Sleep>();
            SleepDetails = new ArrayList<Medix_Pub_Sync_SleepDetail>();
            //snores = new ArrayList<SnoreEntity>();
        }


        JSONArray array1 = new JSONArray();// sleep
        JSONArray array2 = new JSONArray();// sleep_detail
//		JSONArray array3 = new JSONArray();// mix_audio
//		JSONArray array4 = new JSONArray();// snore

        Map<String, Object> PARAMS1 = new HashMap<String, Object>();// sleep
        Map<String, Object> PARAMS2 = new HashMap<String, Object>();// sleep_detail
//        Map<String, Object> PARAMS3 = new HashMap<String, Object>();// mix_audio
//        Map<String, Object> PARAMS4 = new HashMap<String, Object>();// snore




        JSONObject object1 = null, object2 = null; //object3 = null, object4 = null;
        for (Medix_Pub_Sync_Sleep sleep : Sleeps) {
            PARAMS1.put("sleep_id", sleep.getSleepID());
            PARAMS1.put("in_bed_time", sleep.getInBedTime());
            PARAMS1.put("sleep_time", sleep.getSleepTime());
            PARAMS1.put("deep_sleep_time", sleep.getDeepSleepTime());
            PARAMS1.put("sleep_score", sleep.getSleepScore());
            PARAMS1.put("remark", sleep.getRemark());
            PARAMS1.put("wakeup_state", sleep.getWakeupState());
            PARAMS1.put("start_date", sleep.getStartDate());
            PARAMS1.put("end_date", sleep.getEndDate());
            PARAMS1.put("is_deleted", sleep.getIsDeleted() == 1 ? true : false);
            PARAMS1.put("qq_health", sleep.getQqHealth() == 1 ? true : false);
            PARAMS1.put("device_name", sleep.getDeviceName());
            PARAMS1.put("source_type", sleep.getSourceType() == 0 ? 1 : sleep.getSourceType());
            PARAMS1.put("snore_duration", sleep.getSnoreTime());
            PARAMS1.put("snore_count", sleep.getSnoreCount());
            PARAMS1.put("snore_stop_count", sleep.getSnoreStopCount());
            PARAMS1.put("act_count", sleep.getActCount());//
            PARAMS1.put("turn_count", sleep.getTurnCount());
            PARAMS1.put("breath_rate", sleep.getBreathRate());


            object1 = new JSONObject(PARAMS1);
            array1.put(object1);
        }
        Log.e(TAG, "allSleepInfo2String: -------------------------------" + SleepDetails.size());
        for (Medix_Pub_Sync_SleepDetail sleepDetail : SleepDetails) {
            PARAMS2.put("sleep_id", sleepDetail.getSleepID());
            PARAMS2.put("snore_detail_id", sleepDetail.getSleepDetailID());
            PARAMS2.put("snore_duration", sleepDetail.getSleepQuality());
            PARAMS2.put("snore_count", sleepDetail.getSnoreCount());//鼾声次数
            PARAMS2.put("snore_stop_count", sleepDetail.getSnoreStopCount());//止鼾次数
            PARAMS2.put("act_count", sleepDetail.getActCount());
            PARAMS2.put("turn_count", sleepDetail.getTurnCount());
            PARAMS2.put("breath_rate", sleepDetail.getTurnCount());
            PARAMS2.put("record_date", sleepDetail.getCreatedDate());
            PARAMS2.put("is_deleted", sleepDetail.getIsDeleted() == 1 ? true : false);
            object2 = new JSONObject(PARAMS2);
            array2.put(object2);

        }


        Map<String, Object> map = new HashMap<String, Object>();

        map.put("timestamp", MySharedPreferencesUtil.getBodyTime(context));
        map.put("sleep", array1);
        map.put("snore_detail", array2);


        JSONObject object = new JSONObject(map);
        String info = object.toString();
        return info;
    }

}
